package window.component;

import entity.TravelGuideInfo;
import util.HtmlParagraph;
import window.frame.TravelGuideDetailFrame;
import window.tab.HomeTab;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class TravelGuideCard extends JPanel {
    private TravelGuideInfo guide;

    // 构造方法
    public TravelGuideCard(TravelGuideInfo guide, HomeTab homeTab) {
        this.guide = guide;
        this.setLayout(new BorderLayout());
        this.setBorder(new EmptyBorder(10, 10, 10, 10));
        JLabel titleLabel = new JLabel(guide.getTitle());
        titleLabel.setFont(new Font("Arial", Font.BOLD, 18));

        JTextArea contentArea = new JTextArea(3, 30); // 3行，30列
        contentArea.setText(HtmlParagraph.firstSentence(guide.getContent()));
        contentArea.setFont(new Font("Arial", Font.PLAIN, 14));
        contentArea.setLineWrap(true);
        contentArea.setWrapStyleWord(true);
        contentArea.setEditable(false);
        contentArea.setOpaque(false);
        contentArea.setBorder(new EmptyBorder(10, 0, 0, 0));

        ImageIcon imageIcon = new ImageIcon(guide.getImagePath());
        Image image = imageIcon.getImage().getScaledInstance(100, 100, Image.SCALE_SMOOTH);
        JLabel imageLabel = new JLabel(new ImageIcon(image));
        imageLabel.setBorder(new EmptyBorder(0, 0, 0, 10));
        this.add(imageLabel, BorderLayout.WEST);


        JPanel infoPanel = new JPanel();
        infoPanel.setLayout(new BorderLayout());
        ImageIcon icon = new ImageIcon("res/icon/like.png");
        Image likeimage = icon.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
        JLabel like = new JLabel(new ImageIcon(likeimage));
        JLabel likeLabel = new JLabel("Likes: " + guide.getLikeCount());
        JButton detailButton = new JButton("detail");


        JPanel likePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
        likePanel.add(like);
        likePanel.add(likeLabel);


        detailButton.addActionListener(e -> {
            new TravelGuideDetailFrame(guide, homeTab).setVisible(true);
        });
        infoPanel.add(likePanel, BorderLayout.WEST);
        infoPanel.add(detailButton, BorderLayout.EAST);


        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new BorderLayout());
        rightPanel.add(titleLabel, BorderLayout.NORTH);
        rightPanel.add(contentArea, BorderLayout.CENTER);
        rightPanel.add(infoPanel, BorderLayout.SOUTH);
        this.add(rightPanel, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Travel Guide Card");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(700, 150);
        frame.setLayout(new BorderLayout());
        TravelGuideInfo guide = new TravelGuideInfo(
                1, "A Journey to Hangzhou: Wandering West Lake, Savoring the Blend of Classic and Modern"
                , "This is a travel guide for those who want to explore the beautiful city of Hangzhou in China."
                , "res/TravelGuideImage/canton.jpg"
                , 150
        );
        frame.add(new TravelGuideCard(guide, new HomeTab()), BorderLayout.CENTER);
        frame.setVisible(true);
    }
}
